/*
 * @Author: Joker
 * @Date: 2017-11-28 12:18:49
 * @Last Modified by: Joker
 * @Last Modified time: 2018-01-12 09:48:21
 */

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import Iview from 'iview'
import 'iview/dist/styles/iview.css'
import 'element-ui/lib/theme-chalk/index.css'
import './style/cover.css'
import './style/common.css'
import './assets/css/font-awesome.min.css'
import store from './store/index'
// 引入富文本编辑器所用JS
import '../static/UE/ueditor.config.js'
import '../static/UE/ueditor.all.js'
import '../static/UE/lang/zh-cn/zh-cn.js'
import '../static/UE/ueditor.parse.min.js'

Vue.config.productionTip = false

Vue.use(ElementUI)
Vue.use(Iview)

//路由拦截处理
router.beforeEach(async(to, from, next) => {
  //刚进入登录页时，直接放行
  if (from.path === '/' && (to.path === '/' || to.path === '/login') || (to.path === '/' || to.path === '/login')) {
    store.dispatch('clearAll');
    sessionStorage.clear();
    next();
  } else if (to.path === '/' || to.path === '/login') {
    next();
    sessionStorage.clear();
    store.dispatch('clearAll');
  } else {
    if (sessionStorage.getItem('USERTOKEN')) {
      //当前用户拥有的权限
      if (store.getters.userRole === null) {
        let res = await store.dispatch('GetUserPowers');
        if (!res.success) {  
          next('/login');
          return;
        }
      }
      //判断当前用户的所有部门是否以获取
      if (store.getters.userDept === null) {
        let res = await store.dispatch('GetUserDept');
        if (!res.success) {
          next('/login');
          return;
        }
      }
      if (store.getters.userRealDept === null) {
        let res = await store.dispatch('GetUserRealDept');
        if (!res.success) {
          next('/login');
          return;
        }
      }
      //判断拥有角色列表
      if (store.getters.roleList === null) {
        let res = await store.dispatch('GetRoleList');
        if (!res.success) {
          next('/login');
          return;
        }
      }
      //判断拥有角色ID列表
      if (store.getters.roleIdList === null) {
        let res = await store.dispatch('GetRoleIdList');
        if (!res.success) {
          next('/login');
          return;
        }
      }
      //判断拥有角色树形列表
      //   if (store.getters.roleTree === null) {
      //     let res = await store.dispatch('GetRoleTree');
      //   }
      //判断拥有角色可选权限列表
      if (store.getters.rolePowerAll === null) {
        let res = await store.dispatch('GetRolePowerList');
        if (!res.success) {
          next('/login');
          return;
        }
      }
      //项目Id
      if (store.getters.proJectId === null) {
        try {
          let arr = sessionStorage.getItem('MENUACTIVEINDEXARRAY').split(',');
          if (arr instanceof Array) {
            store.commit('SET_PROJECTID', arr[1]);
          }
        } catch (error) {

        }
      }
      next();
    } else {
      next('/login');
      return;
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: {
    App
  }
})

